#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Layout;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;

#endregion

namespace TVC.Modules.Ref
{
    public partial class LOOKUP_ACCNT : ModuleParent, IHostedApplication
    {

        int idAllowEditLanguage = 1;
        int idAddNew = 122;
        int idEdit = 123;
        int idDelete = 124;
        int idTransferIn = 125;
        int idTransferOut = 126;
        int idPrint = 127;
        int idToExcel = 271;

        GridLayout layout = null;
        string TB = "LOOKUP_ACCNT";
        string TBField = "_ING0";
        string strAction = "";

        DTO.PODInfo PODInfo;
        private DataTable _dtb;
        //BUS.TVC_LOOKUPControl lkctr ;
        public LOOKUP_ACCNT()
        {
            _strType = "LOOKUP_ACCNT";
            InitializeComponent();
            _dgvFilter = gridFilter1;
            _dgvList = dgvList;
            PODInfo = (DTO.PODInfo)Session["USER"];
            //lkctr.business = PODInfo.Business_ID;

        }

        public void InitializeApplication()
        {
        }

        //public void AddNew()
        //{

        //}

        public void SetObject(object obj)
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            if (IsPermis(idAddNew))
                objElements.Add(new HostedToolBarButton("Add New", "New", new IconResourceHandle("Add.png"), "C"));
            if (IsPermis(idAddNew))
                objElements.Add(new HostedToolBarButton("Copy", "Copy", new IconResourceHandle("Copy.png"), "O"));
            if (IsPermis(idEdit))
                objElements.Add(new HostedToolBarButton("Edit", "Edit", new IconResourceHandle("Edit.png"), "A"));
            if (IsPermis(idDelete))
                objElements.Add(new HostedToolBarButton("Delete", "Delete", new IconResourceHandle("Delete.png"), "X"));
            if (IsPermis(idAddNew) && IsPermis(idEdit))
                objElements.Add(new HostedToolBarButton("Save", "Save", new IconResourceHandle("Save.png"), "S"));

            objElements.Add(new HostedToolBarButton("Skip", "Skip", new IconResourceHandle("Reject.png"), "K"));
            //if (IsPermis(idAddNew))
            //    objElements.Add(new HostedToolBarButton("Sun", "Insert To SunSystems", new IconResourceHandle("Add.png"), "TS"));

            objElements.Add(new HostedToolBarSeperator());
            if (IsPermis(idTransferIn))
                objElements.Add(new HostedToolBarButton("Transfer In", "Import", new IconResourceHandle("Down.png"), "TI"));
            if (IsPermis(idTransferOut))
                objElements.Add(new HostedToolBarButton("Transfer Out", "Export", new IconResourceHandle("Up.png"), "TO"));
            if (IsPermis(idToExcel))
                objElements.Add(new HostedToolBarButton("To Excel", "To Excel", new IconResourceHandle("Up.png"), "TX"));
            
            //objElements.Add(new HostedToolBarSeperator());
            //if (IsPermis(idPrint))
            //    objElements.Add(new HostedToolBarButton("Print", new IconResourceHandle("Print.png"), "P"));
            objElements.Add(new HostedToolBarButton("Refresh", "Refresh", new IconResourceHandle("Refresh.png"), "R"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        protected override void SetupToolBar(string strAction)
        {
            ToolBar tbarLocal = GetTbarLocal();
            bool on = true, off = false;
            if (tbarLocal == null) return;

            switch (strAction)
            {
                case "C":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TX") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "A":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TX") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "S":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TX") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;

                case "O":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TX") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
                case "V":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TI") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TO") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "TX") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "R") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }
                    break;
            }

        }
        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            try
            {
                sErr = "";
                strAction = (string)objButton.Tag;

                BUS.TVC_LOOKUPControl ctr = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);
                switch (strAction)
                {
                    case "K":
                        skip();
                        break;
                    case "C":
                        AddNew(txtCode);
                        break;
                    case "A":
                        editRecord(txtCode);
                        break;
                    case "S":
                        saveRecord(txtCode, false);
                        break;
                    case "O":
                        copy(txtCode);
                        break;
                    case "X":
                        delete(txtCode);

                        break;
                    case "TI":
                        Forms.frmTransferIn frmTI = new Forms.frmTransferIn(_strType, GetMainForm());
                        frmTI.ShowDialog();
                        break;
                    case "TO":
                        export2Xml();
                        break;
                    case "TX":
                        export2Excel();
                        break;
                    case "R":
                        reloadList();
                        break;
                    case "P":
                        //DTO.OrganizationsInfo info = new DTO.OrganizationsInfo((DataRowView)dgvTVC_LOOKUP.CurrentRow.DataBoundItem);
                        //DataTable dt = DTO.OrganizationsInfo.ToDataTable();
                        //dt.Rows.Add(info.ToDataRow(dt));
                        //DataSet ds = new DataSet();
                        //ds.Tables.Add(dt);
                        //MainForm main = GetMainForm();
                        //if (main != null)
                        //    main.PrintDataSet(ds, _strType);

                        break;
                }
                _processStatus = strAction;
                if (sErr == "")
                    ThrowMsg("Command: " + _processStatus);
            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
        }

        protected override void refreshForm(string str)
        {
            txtCode.Text = "";
            cboDescription.Text = "";
            txtLookup.Text = "";
            cboStatus.Text = "";
        }

        protected override ErrorStatus validateForm(ref string sErr)
        {
            sErr = "";
            string sWarn = "";
            ErrorStatus result = ErrorStatus.SUCCESS;
            sErr = ValidItem();

            if (sErr != "") result = ErrorStatus.ERROR;
            else if (sWarn != "") result = ErrorStatus.WARNING;

            sErr = string.Format("{0}{1}", sErr, sWarn);

            return result;
        }

        private string ValidItem()
        {
            sErr = "";
            if (txtCode.Text == "")
            {
                sErr = "Coud not save blank Code, Please check!";
            }
            //else if (txtLookup.Text == "")
            //{
            //    sErr = "Coud not save blank Lookup, Please check!";
            //}
            return sErr;
        }

        protected override bool checkExists()
        {
            BUS.TVC_LOOKUPControl ctr = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);
            return ctr.IsExist(GetMainForm().SUNDB, TB, TBField, txtCode.Text);
        }

        protected override string addRecord(bool isDraft = false)
        {
            string sErr = "";
            BUS.TVC_LOOKUPControl ctr = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);
            DTO.TVC_LOOKUPInfo info = GetData();
            ctr.Add(GetMainForm().SUNDB, info, ref sErr);
            return sErr;
        }

        protected override string updateRecord(bool isDraft = false)
        {
            BUS.TVC_LOOKUPControl ctr = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);
            DTO.TVC_LOOKUPInfo info = GetData();
            return ctr.Update(GetMainForm().SUNDB, info);
        }

        protected override string delRecord()
        {
            BUS.TVC_LOOKUPControl ctr = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);
            return ctr.Delete(GetMainForm().SUNDB, TB, TBField, txtCode.Text);
        }

        protected override void loadRecord(object o)
        {
            if (o is DataRowView)
            {
                DTO.TVC_LOOKUPInfo inf = new DTO.TVC_LOOKUPInfo((DataRowView)o);
                SetData(inf);
            }
        }

        protected override void enableForm(bool N)
        {
            foreach (Control x in tbdeatil.Controls)
            {
                if (x is TextBox || x is ComboBox || x is TVC.Modules.CustomControl.MultiColumnCombo)
                {
                    x.Enabled = N;
                }
            }
        }

        
        private void LOOKUP_ACCNT_Load(object sender, EventArgs e)
        {
            
            gridFilter1.LoadData(GetMainForm().PODInfo.USER_ID, _strType, PODInfo.Business_ID);
            gridFilter1.panYear.Visible = false;
            gridFilter1.btnOK_Click(sender, e);
        }

        
        private void SetData(DTO.TVC_LOOKUPInfo info)
        {
            txtCode.Text = info.Code;
            cboDescription.Text = info.Description;
            txtLookup.Text = info.Lookup;
            cboStatus.Text = info.Status;
            TB = info.TB;
            TBField = info.TBField;
            txtTBField.Text = info.TBField;
        }        

        private DTO.TVC_LOOKUPInfo GetData()
        {
            return new DTO.TVC_LOOKUPInfo(TB, TBField, txtCode.Text, txtLookup.Text, cboDescription.Text, cboStatus.Text);

        }

        
        private void gridFilter1_GridFiterEvent(string strFilter, int year)// filter
        {
            string sErr = "";
            BUS.TVC_LOOKUPControl ctrLo = new BUS.TVC_LOOKUPControl(PODInfo.Business_ID);

            _dtb = ctrLo.GetSearch(GetMainForm().SUNDB, strFilter, TBField, ref sErr);
            if (!string.IsNullOrEmpty(gridFilter1.SortField))
            {
                _dtb.DefaultView.Sort = gridFilter1.SortField + " " + gridFilter1.Direction;
            }
            bisLOOKUP_ACCNT.DataSource = _dtb;

        }

    }
}